const { Paragraph, TextRun } = require("docx");
const { getAlignment, getIndentValue, getPrefixSymbol, mapFontSizeToHalfPoints } = require("../common/utils");

// 解析字段格式
function parseFieldFormat(format) {
  const segments = [];
  
  if (format.includes('.')) {
    const [integerPart, decimalPart] = format.split('.');
    const integerDigits = parseInt(integerPart) || 0;
    const decimalDigits = parseInt(decimalPart) || 0;
    
    // 添加整数位
    for (let i = 0; i < integerDigits; i++) {
      segments.push({ type: 'digit' });
    }
    
    // 添加小数点
    if (decimalDigits > 0) {
      segments.push({ type: 'decimal' });
    }
    
    // 添加小数位
    for (let i = 0; i < decimalDigits; i++) {
      segments.push({ type: 'digit' });
    }
  } else {
    // 只有整数位
    const digits = parseInt(format) || 0;
    for (let i = 0; i < digits; i++) {
      segments.push({ type: 'digit' });
    }
  }
  
  return segments;
}

// 生成文本输入字段
function generateTextInputField(field, baseIndent = 0) {
  const { value } = field;
  const fieldName = value.fieldName || '';
  const fieldFormat = value.fieldFormat || '3.1';
  const alignment = value.alignment || 'left';
  const sizeHalfPoints = mapFontSizeToHalfPoints(value.fontSize);
  const isBold = value.isBold || false;
  const suffix = value.suffix || '';
  const prefixSymbol = value.prefixSymbol || 'none';
  
  // 计算最终缩进值
  const finalIndent = getIndentValue(alignment, baseIndent);
  const children = [];

  // 添加前缀符号
  const symbol = getPrefixSymbol(prefixSymbol);
  if (symbol) {
    children.push(new TextRun({
      text: symbol,
      size: sizeHalfPoints,
      font: "SimSun"
    }));
  }
  
  // 添加字段名称
  if (fieldName.trim()) {
    children.push(new TextRun({
      text: fieldName,
      size: sizeHalfPoints,
      bold: isBold,
      font: "SimSun" // 宋体
    }));
  }
  
  // 添加空格分隔
  if (fieldName.trim()) {
    children.push(new TextRun({
      text: ' ',
      size: sizeHalfPoints,
      font: "SimSun" // 宋体
    }));
  }
  
  // 解析格式并生成输入框
  const segments = parseFieldFormat(fieldFormat);
  let inDigitGroup = false;
  segments.forEach((segment) => {
    if (segment.type === 'digit') {
      if (!inDigitGroup) {
        children.push(new TextRun({ text: '|', size: sizeHalfPoints, font: "SimSun" }));
        inDigitGroup = true;
      }
      children.push(new TextRun({
        text: '___|',
        size: sizeHalfPoints,
        font: "SimSun"
      }));
    } else if (segment.type === 'decimal') {
      inDigitGroup = false;
      children.push(new TextRun({
        text: ' . ',
        size: sizeHalfPoints,
        font: "SimSun"
      }));
    }
  });

  // 添加后缀
  if (suffix.trim()) {
    children.push(new TextRun({
      text: ` ${suffix}`,
      size: sizeHalfPoints,
      font: "SimSun" // 宋体
    }));
  }
  
  return [
    new Paragraph({
      indent: { left: finalIndent },
      alignment: getAlignment(value.labelAlign || alignment),
      spacing: { 
        before: 100, 
        after: 100,
        line: 360,
        lineRule: "atLeast"
      },
      children: children
    })
  ];
}

module.exports = {
  generateTextInputField
}; 